package com.pirestupppai.adapter;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * 普通viewpager适配
 *
 * @author: xiaoluo
 * @date: 2017-01-23 11:19
 */
public class ViewPagerAdapter extends PagerAdapter {
    private List<View> mList;

    public ViewPagerAdapter() {
        super();
    }

    public ViewPagerAdapter(List<View> list) {
        super();
        this.mList = list;
    }

    public void setList(List<View> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public int getItemPosition(Object object) {
        int length = mList.size();
        for (int i = 0; i < length; i++) {
            if (object.equals(mList.get(i))) {
                return i;
            }
        }
        return super.getItemPosition(object);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(mList.get(position));
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        container.addView(mList.get(position));
        return mList.get(position);
    }
}
